<?php
namespace app\service;
use think\Db;
class shopService{
    public function orderAdd($user_id,$data){
        $userinfo=$this->userFind($user_id);
        $order=$data;
        $shopinfo=Db::table("zz_shop")->where("shop_id",$data["shop_id"])->find();
        $order["shop_name"]=$shopinfo["shop_name"];
        $order["buyer_id"]=$user_id;
        $order["buyer_nickname"]=$userinfo["user_name"];
        $order["buyer_logon_id"]=$userinfo["logon_id"];
        $order["buyer_ip"]=request()->ip();
        $order["uid"]=$user_id;
        $order["goods_money"]=$data["order_money"];
        $order["order_type"]=1;
        $res=Db::table('zz_order_online')->insert($order);
        $order_id =Db::table('zz_order_online')->getLastInsID();
        
        $cartinfo=Db::table("zz_shopcart")->where("shop_id",$data["shop_id"])->where("uid",$user_id)->select();
        foreach ($cartinfo as $key => $value) {
            $id=$value["id"];
            unset($value["id"]);
            unset($value["uid"]);
            unset($value["shop_id"]);
            $order_good=$value;
            $order_good["order_id"]=$order_id ;
            Db::table("zz_order_goods")->insert($order_good);
            Db::table("zz_shopcart")->where("id",$id)->delete();
        }
        return $order_id;
    }





	//根据小区查询物业的付款token
	public function communityToken($community_id){
		$property_id= Db::table('community')->where('community_id',$community_id)->value('property_id');
		return Db::table('property')->where('id',$property_id)->value('shop_ali_token');
	}
	//根据小区查询物业的公司的信息
	public function communityProperty($community_id){
		$property_id= Db::table('community')->where('community_id',$community_id)->value('property_id');
		return Db::table('property')->where('id',$property_id)->find();
	}
	//商家列表
	public function shoplist(){
		return Db::table('zz_shop')->order("shop_id desc")->select();
	}

	//商家详情
	public function shopIdInfo($shop_id){
		return Db::table('zz_shop')->where("shop_id",$shop_id)->find();
	}

	//更新商户信息
	public function shopEdit($data){
		return Db::table('zz_shop')->update($data);
	}

    //商品
    public function goodIdInfo($goods_id){
        return Db::table('zz_goods')->where("goods_id",$goods_id)->find();
    }

    //更新商户信息
    public function goodEdit($data){
        return Db::table('zz_goods')->update($data);
    }

	//添加商户信息
	public function shopAdd($data){
		return Db::table('zz_shop')->insert($data);
	}
	public function shopName($shop_name){
		return Db::table('zz_shop')->where('shop_name',$shop_name)->find();
	}

	public function userEdit($user_id,$data){
		return Db::table("zz_user")->where('user_id',$user_id)->update($data);
	}

	//生活号会员
	public function userlist($where,$page){
		return Db::table('zz_user')->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
	}

	public function userFind($user_id){
		return Db::table('zz_user')->where('user_id',$user_id)->find();
	}
	public function userlistnumber($where){
		return Db::table('zz_user')->where($where)->count();
	}

	//订单列表
	public function orderlist($where,$page){
		return Db::table('zz_order_offline')->where($where)->where('order_status','1')->order('order_id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
	}

	public function shoporderlist($where){
		return Db::table('zz_order_offline')->where($where)->where('order_status','1')->order('order_id desc')->select();
	}
	//订单数量
	public function ordernumber($where){
		return Db::table('zz_order_offline')->where($where)->where('order_status','1')->count();
	}

    public function orderlistonline($where,$page){
        return Db::table('zz_order_online')->where($where)->where('order_status','1')->order('order_id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    //订单数量
    public function ordernumberonline($where){
        return Db::table('zz_order_online')->where($where)->where('order_status','1')->count();
    }

	//根据交易号查询账单信息
    public function tradeOrderInfo($order_no){
        return Db::table('zz_order_offline')->where('order_no',$order_no)->find();
    }
    //更改订单信息
    public function orderEdit($data){
        return Db::table('zz_order_offline')->update($data);
    }

    //转账记录
    public function transferList($where,$page){
    	return Db::table('zz_transfer_record')->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function transfernumber($where){
     	return Db::table('zz_transfer_record')->where($where)->count();
    }
    
    public function transferadd($data){
    	return Db::table('zz_transfer_record')->insert($data);
    }

     //转账记录
    public function billorderlist($where,$page){
    	return Db::table('zz_bill_order')->where($where)->where('order_status','1')->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function billordernumber($where){
     	return Db::table('zz_bill_order')->where($where)->where('order_status','1')->count();
    }
    
    //商家优惠券列表
    public function couponlist($where,$page){
    	return Db::table('zz_coupon_type')->where($where)->order('coupon_type_id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function couponnumber($where){
     	return Db::table('zz_coupon_type')->where($where)->count();
    }

    //sh列表
    public function goodlist($where,$page){
    	return Db::table('zz_goods')->where($where)->order('goods_id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function goodnumber($where){
     	return Db::table('zz_goods')->where($where)->count();
    }

    //sh列表
    public function categorylist($where,$page){
        return Db::table('zz_category')->where($where)->order('category_id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function categorynumber($where){
        return Db::table('zz_category')->where($where)->count();
    }
}